package com.copractice.data.dao.hibernate.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Contains utility methods for using hibernate framework. 
 *
 * @author Dmitry Churbanov (dmitry.churbanov@gmail.com)
 * @version 1.0 12/27/2009
 */
public class HibernateUtil {
    
    private static final Logger LOG = LoggerFactory.getLogger(HibernateUtil.class);

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            //return new Configuration().configure().buildSessionFactory();
            return new AnnotationConfiguration().configure().buildSessionFactory();
        }
        catch (Throwable ex) {
            LOG.error("Initial SessionFactory creation failed: {}", ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    /**
     * Returns <code>SessionFactory</code> class. 
     * 
     * @return <code>SessionFactory</code> class
     */
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
}
